package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    DtsAdminMapper adminMapper;

    @Override
    public DtsAdmin findByUsername(String username) {
        //通过mybatis plus 根据用户名获取 admin对象
        LambdaQueryWrapper<DtsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsAdmin::getUsername, username);

        DtsAdmin admin = adminMapper.selectOne(wrapper);

        return admin;
    }

    @Override
    public Map<String, Object> queryPage(Integer page, Integer limit, String username, String sort, String order) {
        //创建ipage对象
        IPage<DtsAdmin> ipage = new Page<>(page, limit);
        // 封装查询条件
        QueryWrapper<DtsAdmin> wrapper;
        wrapper = new QueryWrapper<>();
        //如果username 不为null， 则添加条件
        if (!StringUtils.isEmpty(username)) {
            wrapper.like(username, username);
        }
        //添加排序条件
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if ("desc".equals(order)) {
                wrapper.orderByDesc(sort);
            } else {
                wrapper.orderByAsc(sort);
            }
        }
        adminMapper.selectPage(ipage, wrapper);

        long total = ipage.getTotal();
        List<DtsAdmin> items = ipage.getRecords();

        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);
        return data;
    }

    @Override
    public DtsAdmin insert(DtsAdmin admin) {
        //对密码进行维护
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(encode);

        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        admin.setDeleted(false);

        //新增操作
        adminMapper.insert(admin);
        return admin;
    }

    BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public DtsAdmin update(DtsAdmin admin) {
        DtsAdmin adminDb = adminMapper.selectById(admin.getId());
        //对比密码是否一致，如果一致，说明不用调整, 如果不相等，说明此次需要设置新的密码
        if (!admin.getPassword().equals(adminDb.getPassword())) {
            admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        }

        admin.setUpdateTime(new Date());
        adminMapper.updateById(admin);
        return admin;
    }

    @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public void delete(DtsAdmin admin) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId, admin.getId());
        Integer integer = brandMapper.selectCount(wrapper);
        if (integer > 0) {
            throw new RuntimeException("管理员被品牌关联不能删除");
        }

        adminMapper.deleteById(admin.getId());
    }


}
